package com.briup.jz.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.briup.jz.bean.BaseConfig;
import com.briup.jz.bean.BaseConfigExample;
import com.briup.jz.dao.BaseConfigMapper;
import com.briup.jz.service.IBaseConfigService;
import com.briup.jz.utils.CustomerException;
@Service
public class BaseConfigServiceImpl implements IBaseConfigService{
	
	@Autowired//不加获得不到对象
	private BaseConfigMapper baseconfigmapper;

	public void saveOrUpdate(BaseConfig baseconfig) throws CustomerException {
		// TODO Auto-generated method stub
		if(baseconfig.getId()!=null) {
			baseconfigmapper.updateByPrimaryKey(baseconfig);//更新
		} else {
			BaseConfigExample example = new BaseConfigExample();
			//多条件复合查询
			example.createCriteria().andNameEqualTo(baseconfig.getName());//创建新的模板
			List<BaseConfig> list = baseconfigmapper.selectByExample(example);
			if(list.size()>0) {
				throw new CustomerException("同名的分类已经出现");
			}
			baseconfigmapper.insert(baseconfig);
		}
		
		
	}
@Override
	public List<BaseConfig> query(String name) {
		// TODO Auto-generated method stub
		//先实例化一个对象
		BaseConfigExample example = new BaseConfigExample();
		if(name!=null) {
			example.createCriteria().andNameLike("%"+name+"%");//模糊查询
		}
		List<BaseConfig> list = baseconfigmapper.selectByExample(example);//??
		return list;
	}
@Override
	public void deleteById(long id) throws CustomerException {
		// TODO Auto-generated method stub
		BaseConfig baseconfig = baseconfigmapper.selectByPrimaryKey(id);//??
		if(baseconfig == null) {
			throw new CustomerException("要删除的信息不存在");//??
		}
		baseconfigmapper.deleteByPrimaryKey(id);//?
	}
	
}
